/**
 * @Author: easten
 * @Created: 2024-07-23 01:58:59
 * @Description: 设备记录表格列定义
 * 不同的设备类型：返回的表头是不一样的
 *
 */
import { IotHistory } from "_common/entity";
import dayjs from "dayjs";
import { PrimaryTableCol, Tag } from "tdesign-react";

// 公共列
export const getCommonColumns = (
  pageState: any
): PrimaryTableCol<IotHistory>[] => {
  return [
    {
      align: "left",
      width: 120,
      ellipsis: true,
      colKey: "orderNo",
      title: "序号",
      cell: ({ rowIndex }) => {
        let _index =
          (pageState.pageNum - 1) * pageState.pageSize + rowIndex + 1;
        return <span>{_index}</span>;
      },
    },
    {
      align: "center",
      ellipsis: true,
      colKey: "deviceCode",
      title: "设备ID",
    },
    /*   {
      align: "center",
      ellipsis: true,
      colKey: "dtCode",
      title: "产品编码",
      cell: ({ row }) => {
        return (
          <Tag theme="primary" variant="light-outline">
            {row.dtCode}
          </Tag>
        );
      },
    }, */
  ];
};

// 加脂机列定义
const getFatColumns = (pageState: any): PrimaryTableCol<IotHistory>[] => {
  return [
    ...getCommonColumns(pageState),
    {
      align: "center",
      ellipsis: true,
      colKey: "value1",
      title: "温度 ℃",
    },
    {
      align: "center",
      ellipsis: true,
      colKey: "value5",
      title: "电压 V",
    },
    {
      align: "center",
      ellipsis: true,
      colKey: "value2",
      title: "电流 mA",
    },
    {
      align: "center",
      ellipsis: true,
      colKey: "value3",
      title: "剩余加脂次数",
    },
    {
      align: "center",
      width: 100,
      ellipsis: true,
      colKey: "value4",
      title: "是否油堵",
      cell: ({ row }) => {
        return row.value4 == 1 ? (
          <Tag theme="warning" variant="outline">
            是
          </Tag>
        ) : (
          <Tag theme="success" variant="outline">
            否
          </Tag>
        );
      },
    },
    {
      align: "center",
      ellipsis: true,
      colKey: "time",
      title: "监测时间",
      cell: ({ row }) => {
        return dayjs(row.time).format("YYYY-MM-DD HH:mm:ss");
      },
    },
  ];
};

// 温湿度
const getTempColumns = (pageState: any): PrimaryTableCol<IotHistory>[] => {
  return [...getCommonColumns(pageState), ...getWsdBaseCol()];
};

const getWsdBaseCol = (): PrimaryTableCol<IotHistory>[] => {
  return [
    {
      width: 100,
      align: "center",
      ellipsis: true,
      colKey: "value1",
      title: "温度 ℃",
    },
    {
      width: 100,
      align: "center",
      ellipsis: true,
      colKey: "value2",
      title: "湿度 %",
    },
    {
      width: 100,
      align: "center",
      ellipsis: true,
      colKey: "value3",
      title: "电池电压",
      cell: ({ row }) => {
        return <>{row.value3 == 0 ? "--" : row.value3}</>;
      },
    },
    {
      align: "center",
      ellipsis: true,
      colKey: "time",
      title: "监测时间",
      cell: ({ row }) => {
        return dayjs(row.time).format("YYYY-MM-DD HH:mm:ss");
      },
    },
  ];
};

// 水浸设备
const getWaterColumns = (pageState: any): PrimaryTableCol<IotHistory>[] => {
  return [...getCommonColumns(pageState), ...getSjBaseCol()];
};
const getSjBaseCol = (): PrimaryTableCol<IotHistory>[] => {
  return [
    {
      align: "center",
      width: 130,
      ellipsis: true,
      colKey: "value1",
      title: "水浸状态",
      cell: ({ row }) => {
        // 0  离线、1 正常、2 报警，使用tag
        return row.value1 == 0 ? (
          <Tag theme="default" variant="light-outline">
            离线
          </Tag>
        ) : row.value1 == 1 ? (
          <Tag theme="success" variant="light-outline">
            正常
          </Tag>
        ) : (
          <Tag theme="danger" variant="light-outline">
            报警
          </Tag>
        );
      },
    },
    {
      align: "center",
      width: 130,
      ellipsis: true,
      colKey: "value2",
      title: "电池电压V",
      cell: ({ row }) => {
        return <>{row.value2 == 0 ? "--" : row.value2}</>;
      },
    },
    {
      align: "center",
      ellipsis: true,
      colKey: "time",
      title: "监测时间",
      cell: ({ row }) => {
        return dayjs(row.time).format("YYYY-MM-DD HH:mm:ss");
      },
    },
  ];
};

// 16路烟感设备
const getSmokeColumns = (pageState: any): PrimaryTableCol<IotHistory>[] => {
  return [...getCommonColumns(pageState), ...getYgBaseCol()];
};

const getYgBaseCol = (): PrimaryTableCol<IotHistory>[] => {
  return [
    {
      align: "center",
      ellipsis: true,
      colKey: "value1",
      title: "烟感状态",
      cell: ({ row }) => {
        // 0  离线、1 正常、2 报警，使用tag
        return row.value1 == 0 ? (
          <Tag theme="success" variant="light-outline">
            正常
          </Tag>
        ) : row.value1 == 1 ? (
          <Tag theme="danger" variant="light-outline">
            报警
          </Tag>
        ) : (
          <Tag theme="warning" variant="light-outline">
            其他
          </Tag>
        );
      },
    },
    {
      align: "center",
      ellipsis: true,
      colKey: "time",
      title: "监测时间",
      cell: ({ row }) => {
        return dayjs(row.time).format("YYYY-MM-DD HH:mm:ss");
      },
    },
  ];
};

// 无线温度
const getWirelessColumns = (pageState: any): PrimaryTableCol<IotHistory>[] => {
  return [...getCommonColumns(pageState), ...getWdBaseCol()];
};

const getWdBaseCol = (): PrimaryTableCol<IotHistory>[] => {
  return [
    {
      align: "center",
      ellipsis: true,
      colKey: "value1",
      title: "温度 ℃",
      cell: ({ row }) => {
        return <>{row.value1}</>;
      },
    },
    {
      align: "center",
      ellipsis: true,
      colKey: "value2",
      title: "电池电压 V",
      cell: ({ row }) => {
        return <>{row.value2 == 0 ? "--" : row.value2}</>;
      },
    },
    {
      align: "center",
      ellipsis: true,
      colKey: "time",
      title: "监测时间",
      cell: ({ row }) => {
        return dayjs(row.time).format("YYYY-MM-DD HH:mm:ss");
      },
    },
  ];
};

//

export const GetColumns = (
  dtCode: number,
  pageState: any
): PrimaryTableCol<IotHistory>[] => {
  switch (dtCode) {
    case 109:
      return getFatColumns(pageState);
    case 112:
      return getTempColumns(pageState);
    case 114:
      return getWaterColumns(pageState);
    case 111:
      return getSmokeColumns(pageState);
    case 116:
      return getWirelessColumns(pageState);
    case 108:
      return getWdBaseCol();
    default:
      return [];
  }
};
export const GetBaseColumns = (dtCode: number) => {
  switch (dtCode) {
    case 109:
      return getFatColumns;
    case 112:
      return getWsdBaseCol();
    case 114:
      return getSjBaseCol();
    case 111:
      return getYgBaseCol();
    case 116:
      return getWdBaseCol();
    case 108:
      return getWdBaseCol();
    default:
  }
};
export type DtColumnType = {};
